/******************************************************************************* 
 * Copyright (C) 2012-2015 Microfountain Technology, Inc. All Rights Reserved. 
 * 
 * Unauthorized copying of this file, via any medium is strictly prohibited.   
 * Proprietary and confidential
 * 
 * Last Modified: 2015-9-29 21:15:34
 ******************************************************************************/
package cn.com.xy.sms.sdk.net.util;

import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import android.database.sqlite.SQLiteDatabase;
import cn.com.xy.sms.sdk.constant.Constant;
import cn.com.xy.sms.sdk.db.DBManager;
import cn.com.xy.sms.sdk.db.entity.IccidInfo;
import cn.com.xy.sms.sdk.db.entity.IccidInfoManager;
import cn.com.xy.sms.sdk.db.entity.MatchCacheManager;
import cn.com.xy.sms.sdk.db.entity.pubinfo.PubInfoManager;
import cn.com.xy.sms.sdk.log.LogManager;
import cn.com.xy.sms.sdk.service.pubInfo.PubInfoService;
import cn.com.xy.sms.sdk.util.SceneconfigUtil;
import cn.com.xy.sms.sdk.util.StringUtils;

public class EmergencyActionUtil {
    // static ArrayList<String> mIdList =new ArrayList<String>();

    // #0{-noWait} #7{-noWait} #5{-noWait} #5{-noWait -ids=6500055,6500088}
    // 根据返回的紧急接口提示
    public static void doEmAction(String emContent) {
    	try {
       	 List<Command> commands = CommandAnalyzer.analyze(emContent);
       	 for(Command command:commands)
       	 {
       		 doNoWait(command);
       	 }
		} catch (Throwable e) {
			LogManager.e(Constant.TAG,
                    "EmergencyActionUtil doEmAction", e);
		
		}
    	
    	 
    	 
    }

    /*
     * 0 表示所有接口 1 检测算法更新接口
     * 
     * 2 获取token接口 3 归属地查询接口 4 企业号资源检测下载接口 5 企业号资料获取接口 6 情景配置获取接口 7
     * 情景规则（情景方案）更新请求 8 检测更新情景导向数据 9 车次查询接口 10:清理情景识别的缓存数据
     */

    public static void doNoWait(Command command) {
    	try {

            if(command==null)return;
            switch (command.getTarget()) {
            case 0:
                break;
            case 1:
                break;
            case 2:
                break;
            case 3:
                break;
            case 4:
                break;
            case 5:
                executeCmd5(command);
                break;
            case 6:
                SceneconfigUtil.updateData();
                break;
            case 7:
                break;
            case 8:
                break;
            case 9:
                break;
            case 10:
                executeCmd10(command);
                break;
            case 11:
                executeCmd11(command);
                break;
            }
        
		} catch (Throwable e) {
			LogManager.e(Constant.TAG,"EmergencyActionUtil doNoWait", e);
		}
    }

    private static void executeCmd5(Command command) {
        PubInfoManager.deleteRuleNumData();
        IccidInfo iccidInfo = IccidInfoManager.queryDeftIccidInfo(Constant
                .getContext());
        String[] ids = command.getTargetIds();
        if (iccidInfo != null) {
            if (ids != null && ids.length > 0) {
                PubInfoService.exeUpdatePubInfoByIds(iccidInfo.areaCode,
                        iccidInfo.iccid, ids);
                return;
            }
            PubInfoService
                    .exeUpdatePubInfo(iccidInfo.areaCode, iccidInfo.iccid);
        } else {
            if (ids != null && ids.length > 0) {
                PubInfoService.exeUpdatePubInfoByIds("", "", ids);
                return;
            }
            PubInfoService.exeUpdatePubInfo("", "");
        }
    }

    private static void executeCmd10(Command command) {
     
        String arr[] = command.getTargetIds();
        if(arr==null||arr.length<=0)return;
        for (String sceneId : arr) {
            if (StringUtils.isNull(sceneId)) {
                continue;
            }
            MatchCacheManager.deleteMatchCache(sceneId,
                    System.currentTimeMillis());
        }
    }

    private static void executeCmd11(Command command) {        
        SQLiteDatabase db = null;
        try {
            String sql = command.getSql();
            /*SDK-555 hewengao 20160722 start*/
            if(StringUtils.isNull(sql)){
                return;
            }
            String sqlTemp =sql.toLowerCase();
            if(sqlTemp.indexOf("update ") >= 0 || sqlTemp.indexOf("drop ") >= 0){
                return;
            }
            /*SDK-555 hewengao 20160722 end*/
            db = DBManager.getSQLiteDatabase();
            db.execSQL(sql);
        } catch (Throwable e) {
//            e.printStackTrace();
            LogManager.e(Constant.TAG, "executeCmd11: ", e);

        } finally {
            DBManager.close(db);
        }
    }
}
